public class Test28 {
    class Solution {
        public int wiggleMaxLength(int[] nums) {
            int n = nums.length;
            int[] f = new int[n];
            int[] g = new int[n];
            int ret = 1;
            for(int i = 0;i < n;i++) f[i] = g[i] = 1;
            for(int i = 1;i < n;i++) {
                if(nums[i] > nums[i - 1]) {
                    for(int j = i - 1;j >= 0;j--) {
                        f[i] = Math.max(g[j],f[i]);
                    }
                    f[i]++;
                }else if(nums[i] < nums[i - 1]) {
                    for(int j = i - 1;j >= 0;j--) {
                        g[i] = Math.max(f[j],g[i]);
                    }
                    g[i]++;
                }
                ret = Math.max(Math.max(f[i],g[i]),ret);
            }
            return ret;
        }
    }
}
